package cn.edu.nuc.android.oweibo.adapters;

import java.lang.ref.SoftReference;
import java.net.URL;
import java.util.HashMap;
import java.util.List;

import weibo4android.Status;
import android.content.Context;
import android.graphics.drawable.Drawable;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import cn.edu.nuc.android.oweibo.R;
import cn.edu.nuc.android.oweibo.layouts.StatusListItem;

public class StatusListAdapter extends ArrayAdapter<Status> {
	
	private Context context;
	private HashMap<URL, SoftReference<Drawable>> imageCache;

	public StatusListAdapter(Context context, List<Status> statii) {
		super(context, R.layout.status_list_item, statii);
		this.context = context;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		StatusListItem sli;
		if (null == convertView) {
			sli = (StatusListItem)View.inflate(context, R.layout.status_list_item, null);
		} else {
			sli = (StatusListItem)convertView;
		}
		sli.setStatus((Status)getItem(position));
		return sli;
	}

	/**
	 * 获取当前微薄的第一个ID
	 * @return
	 */
	public long getFirstId() {
		Status firstStatus = getItem(0);
		if (null == firstStatus) {
			return 0;
		} else {
			return firstStatus.getId();
		}
	}

	/**
	 * 获取当前微薄的最后一个ID
	 * @return
	 */
	public long getLastId() {
		Status lastStatus = getItem(getCount() - 1);
		if (null == lastStatus) {
			return 0;
		} else {
			return lastStatus.getId();
		}
	}

	/**
	 * 添加新微薄
	 * @param statii
	 */
	public void appendNewer(List<Status> statii) {
		setNotifyOnChange(false);
		for (Status status : statii) {
			insert(status, 0);
		}
		notifyDataSetChanged();
	}

	/**
	 * 添加老微薄
	 * @param statii
	 */
	public void appendOlder(List<Status> statii) {
		setNotifyOnChange(false);
		for (Status status : statii) {
			add(status);
		}
		notifyDataSetChanged();
	}


}
